var ReservationsInfoDlg = {reservationsInfoData: {}, zTreeInstance: null, validateFields: {}};
var windowNameIndex = parent.layer.getFrameIndex(window.name); //获取窗口索引

ReservationsInfoDlg.clearData = function () {
    this.reservationsInfoData = {};
};
// 设置对话框中的数据 @param key 数据的名称 @param val 数据的具体值
ReservationsInfoDlg.set = function (key, val) {
    this.reservationsInfoData[key] = (typeof value == "undefined") ? $("#" + key).val() : value;
    return this;
};

ReservationsInfoDlg.adminiGetFollower = function () {
    var index = layer.open({
        type: 2,
        title: '添加跟进人',
        area: ['70%', '60%'],
        fix: true,
        maxmin: true,
        content: Feng.ctxPath + '/reservations/to_followers'
    });
    this.layerIndex = index;
    document.getElementById("isEffective").value = 3;
};

ReservationsInfoDlg.adminiGetBelonger = function () {
    var index = layer.open({
        type: 2,
        title: '添加跟进人',
        area: ['70%', '60%'],
        fix: true,
        maxmin: true,
        content: Feng.ctxPath + '/reservations/to_belongers'
    });
    this.layerIndex = index;
    document.getElementById("isEffective").value = 4;
};

ReservationsInfoDlg.get = function (key) {
    return $("#" + key).val();
};

ReservationsInfoDlg.close = function () {
    parent.layer.close(windowNameIndex);
};

ReservationsInfoDlg.getEdit = function () {
    var filingTime = $("#filingTime").val();
    var follower = $("#follower").val();
    var userName = $("#userName").val();
    var roleName = $("#roleName").val();
    var statusValue = $("#statusValue").val();
    if (roleName == "网电主管") {
        if (statusValue == '预约') {
            $(".directorSelect").attr('disabled', false).css('border', '1px solid #999999');
            $(".directorInput").attr('readOnly', false).css('border', '1px solid #999999');
            $("#customerAge").attr("readOnly", false).css('border', '1px solid #999999');
            // $("#remarks,#content").attr("readOnly", false);
            if ($("#appointmentPayService").val() == "") {
                $("#appointmentPayService").attr('disabled', false).css('border', '1px solid #999999');
            }
            if ($("#accessDisease").val() == "") {
                $("#accessDisease").attr('disabled', false).css('border', '1px solid #999999');
            }
        } else if (statusValue == '到店') {
            $(".partSelect").attr('disabled', false).css('border', '1px solid #999999');
            $(".partInput").attr('readOnly', false).css('border', '1px solid #999999');
            // $("#remarks,#content").attr("readOnly", false);
            $("#customerAge").attr("readOnly", false).css('border', '1px solid #999999');
            if ($("#appointmentPayService").val() == "") {
                $("#appointmentPayService").attr('disabled', false).css('border', '1px solid #999999');
            }
            if ($("#accessDisease").val() == "") {
                $("#accessDisease").attr('disabled', false).css('border', '1px solid #999999');
            }
        }
    } else {
        if (userName == follower && statusValue == '预约') {
            if (new Date(filingTime).toDateString() === new Date().toDateString()) {
                // $("#remarks,#content").attr("readOnly", false);
                $(".select_styles").attr('disabled', false).css('border', '1px solid #999999');
                $("#customerAge,#customerName,#qqNumber,#wechatNumber,#keyWord,#obstaclePoint").attr("readOnly", false).css('border', '1px solid #999999');
                if ($("#appointmentPayService").val() == "") {
                    $("#appointmentPayService").attr('disabled', false).css('border', '1px solid #999999');
                }
                if ($("#accessDisease").val() == "") {
                    $("#accessDisease").attr('disabled', false).css('border', '1px solid #999999');
                }
            } else {
                $(".partSelect").attr('disabled', false).css('border', '1px solid #999999');
                $(".partInput").attr('readOnly', false).css('border', '1px solid #999999');
                // $("#remarks,#content").attr("readOnly", false);
                $("#customerAge,#customerName,#qqNumber,#wechatNumber,#keyWord,#obstaclePoint").attr("readOnly", false).css('border', '1px solid #999999');
                if ($("#appointmentPayService").val() == "") {
                    $("#appointmentPayService").attr('disabled', false).css('border', '1px solid #999999');
                }
                if ($("#accessDisease").val() == "") {
                    $("#accessDisease").attr('disabled', false).css('border', '1px solid #999999');
                }
            }
        } else if (userName == follower && statusValue == '到店') {
            $(".partSelect").attr('disabled', false).css('border', '1px solid #999999');
            $(".partInput").attr('readOnly', false).css('border', '1px solid #999999');
            // $("#remarks,#content").attr("readOnly", false);
            $("#customerAge").attr("readOnly", false).css('border', '1px solid #999999');
            if ($("#appointmentPayService").val() == "") {
                $("#appointmentPayService").attr('disabled', false).css('border', '1px solid #999999');
            }
            if ($("#accessDisease").val() == "") {
                $("#accessDisease").attr('disabled', false).css('border', '1px solid #999999');
            }
        }
    }
};

ReservationsInfoDlg.getEditto = function () {
    $(".partSelect").attr('disabled', true).css('border', '0px solid #999999');
    $(".partInput").attr('readOnly', true).css('border', '0px solid #999999');
    $("#customerAge").attr('readOnly', true).css('border', '0px solid #999999');
    $("#remarks").attr("readOnly", true);
    $("#content").attr("readOnly", true);
    $(".directorSelect").attr('disabled', true).css('border', '0px solid #999999');
    $(".directorInput").attr('readOnly', true).css('border', '0px solid #999999');
};

ReservationsInfoDlg.deleteReservation = function () {
    var r = confirm("是否删除该用户")
    if (r == true) {
        this.clearData();
        this.collectData();
        if (!this.validate()) {
            return;
        }
        var ajax = new $ax(Feng.ctxPath + "/reservations/deleteReservation", function (data) {
            Feng.success("删除成功!");
            window.parent.ReservationsRecordTable.search();
            ReservationsInfoDlg.close()
        }, function (data) {
            alert("没有权限，请联系系统管理员！");
            parent.layer.close(windowNameIndex);
        });
        ajax.set(this.reservationsInfoData);
        ajax.start();
    }
};

ReservationsInfoDlg.collectData = function () {
    this.set('id').set('customerName').set('customerPhone').set('customerPhone2').set('customerSex').set('customerAge').set('economicAbility').set('address').set('consumAbility').set('consumptionHabits')
        .set('qqNumber').set('wechatNumber').set('occupation').set('archiving').set('filingTime').set('statuss').set('maritalStatus').set('isHaveChildren').set('characters')
        .set('entryClerk').set('obstaclePoint').set('oneChannel').set('twoChannel').set('thrChannel').set('oneMedium').set('twoMedium').set('thrMedium').set('appointmentArrivalTime').set('appointmentPayService')
        .set('isLarge').set('keyWord').set('department').set('followers').set('content').set('reservationsNum').set('otherContact').set('appointmentTimes')
        .set('accessDisease').set('remarks').set('classification').set('status').set('outpatientName').set('phoneSharShip').set('userName');
};

ReservationsInfoDlg.getArchivings = function () {
    var index = layer.open({
        type: 2,
        title: '选中建档人',
        area: ['90%', '90%'], //宽高
        fix: true, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/reservation/to_archiving'
    });
    this.layerIndex = index;
};

ReservationsInfoDlg.getEditfs = function () {
    $(".modify2").attr("readOnly", false);
    $(".modify2").attr("disabled", false);
    $(".modify2").css("border", "1px solid #999999");
};

ReservationsInfoDlg.validate = function () {
    $('#reservationsInfoForm').data("bootstrapValidator").resetForm();
    $('#reservationsInfoForm').bootstrapValidator('validate');
    return $("#reservationsInfoForm").data('bootstrapValidator').isValid();
};

ReservationsInfoDlg.modifyChannel = function () {
    var id = $("#id").val();
    var outpatientName = $("#outpatientName").val();
    var ido = id + "," + outpatientName;
    var index = layer.open({
        type: 2,
        title: '修改渠道',
        area: ['70%', '80%'], //宽高
        fix: true, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/reservations/reservations_modifyChannel/' + ido
    });
    this.layerIndex = index;
};

ReservationsInfoDlg.addSubmit = function () {
    this.clearData();
    this.collectData();
    var appointmentPayService = $('#appointmentPayService').val();
    var customerSex = $('input[name="sex"]:checked').val();
    var customerName = $("#customerName").val(); //姓名
    var customerPhone = $("#customerPhone").val(); //电话
    var appointmentArrivalTime = $("#appointmentArrivalTime").val(); //预约时间
    var outpatientName = $("#outpatientName").val(); //门诊
    var oneChannel = $("#oneChannel").val();
    var twoChannel = $("#twoChannel").val();
    var thrChannel = $("#thrChannel").val();
    var accessDisease = $("#accessDisease").val();
    var statuss = $("#statuss").val();
    var remarks = $("#remarks").val();
    if (!this.validate()) {
        return;
    }
    if (customerName == null || customerName == "") {
        alert("请输入姓名！");
        return;
    }
    if (customerPhone == null || customerPhone == "") {
        alert("请输入电话！");
        return;
    }
    if (statuss == null || statuss == "") {
        alert("请选择状态！");
        return;
    }
    if (appointmentPayService == null || appointmentPayService == "") {
        alert("请选择预约项目！");
        return;
    }
    if (appointmentArrivalTime == null || appointmentArrivalTime == "") {
        alert("请选择预约时间！");
        return;
    }
    if (outpatientName == null || outpatientName == "") {
        alert("请选择门诊！");
        return;
    }
    if (oneChannel == null || oneChannel == "" || twoChannel == null || twoChannel == "" || thrChannel == null || thrChannel == "") {
        alert("请选择渠道！");
        return;
    }
    if (oneChannel != "直接上门" && accessDisease == "") {
        alert("请选择访问病种！");
        return;
    }
    if ($('#customerAge').val().length > 3) {
        alert("年龄长度应小于3");
        return;
    }
    if (remarks == null || remarks == "") {
        alert("填写客户详情！");
        return;
    }
    this.reservationsInfoData["customerSex"] = customerSex;
    var ajax = new $ax(Feng.ctxPath + "/reservation/add", function (data) {
        if (data.split("#")[0] == 1) {
            Feng.success("添加成功!");
            window.parent.ReservationsRecordTable.search();
            ReservationsInfoDlg.close();
        } else {
            alert("客户电话已存在");
        }
    }, function (data) {
        alert("没有权限，请联系系统管理员！");
        parent.layer.close(windowNameIndex);
    });
    ajax.set(this.reservationsInfoData);
    ajax.start();
};

ReservationsInfoDlg.editSubmit = function () {
    this.clearData();
    this.collectData();
    var status = $('#status').val();
    var appointmentPayService = $('#appointmentPayService').val();
    var oneChannel = $("#oneChannel").val();
    var twoChannel = $("#twoChannel").val();
    var thrChannel = $("#thrChannel").val();
    var remarks = $('#remarks').val();
    if (!this.validate()) {
        return;
    }
    if (status == "到店") {
        alert("已到店，如需修改请联系管理员！");
        return;
    }
    if (appointmentPayService == null || appointmentPayService == "") {
        alert("请选择预约项目！");
        return;
    }
    if (oneChannel == null || oneChannel == "请选择" || twoChannel == null || twoChannel == "请选择" || thrChannel == null || thrChannel == "请选择") {
        alert("请选择渠道！");
        return;
    }
    if (remarks == null || remarks == "") {
        alert("请填写客户详情！");
        return;
    }
    var phone2 = $('#customerPhone2').val();
    if (phone2 == null || phone2 == "") {
        phone2 = "phone2";
    }
    var belonger1 = $('#belonger1').val();
    var belonger2 = $('#belonger2').val();
    var belonger = belonger2;
    if (belonger1 == null || belonger1 == "") {
        belonger = belonger2;
    } else if (belonger2 == null || belonger2 == "") {
        belonger = belonger1;
    } else {
        belonger = belonger1;
    }
    if (belonger == null || belonger == "") {
        belonger = "没人";
    }
    var cri = phone2 + "," + belonger;
    var ajax = new $ax(Feng.ctxPath + "/reservations/update/" + cri, function (data) {//提交信息
        if (data == 1) {
            if (window.parent.document.getElementById("pageType").value == 0) {
                Feng.success("修改成功!");
                window.parent.ReservationsRecordTable.search();
                ReservationsInfoDlg.getEditto();
            }
            if (window.parent.document.getElementById("pageType").value == 1) {
                Feng.success("修改成功!");
                window.parent.ReservationsArrivedTable.table.refresh();
                ReservationsInfoDlg.getEditto();
            }
            if (window.parent.document.getElementById("pageType").value == 2) {
                Feng.success("修改成功!");
                window.parent.ReservationsNoArrivedTable.table.refresh();
                ReservationsInfoDlg.getEditto();
            }
        } else {
            alert("客户电话已存在");
        }
    }, function (data) {
        alert("没有权限，请联系系统管理员！");
        parent.layer.close(windowNameIndex);
    });
    ajax.set(this.reservationsInfoData);
    ajax.start();
};

ReservationsInfoDlg.addChatRecord = function () {
    this.clearData();
    this.collectData();
    if (!this.validate()) {
        return;
    }
    var ajax = new $ax(Feng.ctxPath + "/reservations/addChatRecord", function (data) {//提交信息
        Feng.success("添加成功!");
        window.parent.ReservationsRecordTable.search();
        ReservationsInfoDlg.close();
    }, function (data) {
        alert("没有权限，请联系系统管理员！");
        parent.layer.close(windowNameIndex);
    });
    ajax.set(this.reservationsInfoData);
    ajax.start();
};

ReservationsInfoDlg.getArchiving = function () {/*建档，跟进点击*/
    var index = layer.open({
        type: 2,
        title: '添加建档人',
        area: ['90%', '90%'], //宽高
        fix: true, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/reservations/to_archiving'
    });
    this.layerIndex = index;
};

ReservationsInfoDlg.getFollower = function () {
    var index = layer.open({
        type: 2,
        title: '添加跟进人',
        area: ['70%', '60%'], //宽高
        fix: true, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/reservations/to_follower'
    });
    this.layerIndex = index;
};

ReservationsInfoDlg.initData = function () {//初始化数据
    var seletecustomerSex = $('#customerSexValue').val();
    var seletemaritalStatus = $('#maritalStatusValue').val();
    var seleteisLarge = $('#isLargeValue').val();
    var seletestatus = $('#statusValue').val();
    var seletephoneSharShip = $('#phoneSharShipValue').val();
    var seleteaccessDisease = $('#accessDiseaseValue').val();
    if ($('#seletecustomerSex').val() != "") {
        $("#customerSex option[value='" + seletecustomerSex + "']").attr("selected", "selected");
    }
    if ($('#seletecustomerSex').val() != "") {
        $("#select1 option[value='" + seletecustomerSex + "']").attr("selected", "selected");
    }
    if ($('#seletemaritalStatus').val() != "") {
        $("#maritalStatus option[value='" + seletemaritalStatus + "']").attr("selected", "selected");
    }
    if ($('#seleteisLarge').val() != "") {
        $("#isLarge option[value='" + seleteisLarge + "']").attr("selected", "selected");
    }
    if ($('#seletestatus').val() != "") {
        $("#status option[value='" + seletestatus + "']").attr("selected", "selected");
    }
    if ($('#seletephoneSharShip').val() != "" && seletephoneSharShip != "") {
        $("#phoneSharShip option[value='" + seletephoneSharShip + "']").attr("selected", "selected");
    }
    if ($('#seleteaccessDisease').val() != "" && seleteaccessDisease != "") {
        $("#accessDisease option[value='" + seleteaccessDisease + "']").attr("selected", "selected");
    }
};

function checkDate() {
    var date = new Date();
    date.setDate(date.getDate() - 1);
    var now = date.valueOf();
    var time = new Date($("#appointmentArrivalTime").val()).valueOf();
    if (now > time) {
        layer.alert("预约时间不能小于当前时间！");
        $("#appointmentArrivalTime").val("");
    }
};

$(function () {
    Feng.initValidator("reservationsInfoForm", ReservationsInfoDlg.validateFields);
    ReservationsInfoDlg.initData();
    var department = document.getElementById("department").value;
    var seleteoccupation = $('#occupationValue').val();
    if (seleteoccupation != "") {
        $("#occupation option[value='" + seleteoccupation + "']").attr("selected", "selected");
    }
    var seleteconsumptionHabits = $('#consumptionHabitsValue').val();
    if (seleteconsumptionHabits != "") {
        $("#consumptionHabits option[value='" + seleteconsumptionHabits + "']").attr("selected", "selected");
    }
    var seletecharacters = $('#charactersValue').val();
    if (seletecharacters != "") {
        $("#characters option[value='" + seletecharacters + "']").attr("selected", "selected");
    }
    var seletvehicle = $('#vehicleValue').val();
    if ($('#id').val() != "" && seletvehicle != "") {
        $("#vehicle option[value='" + seletvehicle + "']").attr("selected", "selected");
    }
    var seleteconomicAbility = $('#economicAbilityValue').val();
    if ($('#id').val() != "" && seleteconomicAbility != "") {
        $("#economicAbility option[value='" + seleteconomicAbility + "']").attr("selected", "selected");
    }
    var seleteooutpatientName = $('#outpatientNameValue').val();
    if ($('#id').val() != "" && seleteooutpatientName != "") {
        $("#outpatientName option[value='" + seleteooutpatientName + "']").attr("selected", "selected");
    }
    var seletepayservicename = $('#payservicenameValue').val();
    if ($('#id').val() != "" && seletepayservicename != "") {
        seletepayservicename = seletepayservicename.split(",")[0];
        $("#appointmentPayService option[value='" + seletepayservicename + "']").attr("selected", "selected");
    }
    $.post(Feng.ctxPath + "/channel/getChannelssd", function (data) { //初始化下拉渠道列表
        ChannelList = data;
        channelInit('oneChannel', 'twoChannel', 'thrChannel');
        if (oneChanneli == "直接上门" || oneChanneli == "老带新") {
            $("#oneChannel").attr("disabled", true);
            $("#twoChannel").attr("disabled", true);
            $("#thrChannel").attr("disabled", true);
        }
    }, "json");
    $.post(Feng.ctxPath + "/medium/getMedium", function (data) {
        MediumList = data;
        mediumInit('oneMedium', 'twoMedium', 'thrMedium');
    }, "json");//初始化下拉媒介列表
    var arr = $("#huifang").val();
    var strs = arr.split(",");
    var str1 = '';
    for (var i = 0; i < strs.length; i++) {
        str1 = "<p>" + strs[i] + "</p>";
        if (strs[i] != "") {
            $("#further").append(str1);
        }
    }
    $(".modify").attr("readOnly", "true");
    $(".select_styles").attr("disabled", "disabled");
    if ($("#followers").val() != "" && $("#followers").val() != null && $("#followers").val() != undefined && $("#followers").val() != "待定") {
        $("#aaa").hide();
    } else {
        $("#aaa").show();
    }
    if (department == "公共池") {
        document.getElementById("isEffective").value = 1;
        $("#effective").show();
        $("#noEffective").show();
        $("#bbb").show();
        $("#ccc").show();
        $("#ddd").show();
    } else {
        $("#effective").hide();
        $("#noEffective").hide();
        $("#bbb").hide();
        $("#ccc").hide();
        $("#ddd").hide();
    }
    trackStatus = function (obj) {
        if ($(obj).val() == "过期") {
            $("#appointmentTime").attr('disabled', true);
        } else {
            $("#appointmentTime").attr('disabled', false);
        }
    };
});